package io.github.uicdb.et.recipe.handler;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.uicdb.et.ModItemDataComponents;
import io.github.uicdb.et.ModTurnInRecipeHandlerTypes;
import io.github.uicdb.et.element.BasicElement;
import io.github.uicdb.et.item_data.ItemElementData;
import io.github.uicdb.et.recipe.TurnInRecipeHandler;
import io.github.uicdb.et.recipe.TurnInRecipeHandlerType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.minecraft.core.Holder;
import net.minecraft.world.item.ItemStack;

@RequiredArgsConstructor
@Getter
public class AttachElementHandler extends TurnInRecipeHandler {
    final Holder<BasicElement> element;
    final int amount;
    final int attachTime;

    public static final MapCodec<AttachElementHandler> CODEC = RecordCodecBuilder.mapCodec(
            i->i.group(
                    BasicElement.HOLDER_CODEC.fieldOf("element").forGetter(AttachElementHandler::getElement),
                    Codec.intRange(0,Integer.MAX_VALUE).fieldOf("amount").forGetter(AttachElementHandler::getAmount),
                    Codec.intRange(0,Integer.MAX_VALUE).fieldOf("attachTime").forGetter(AttachElementHandler::getAttachTime)
            ).apply(i, AttachElementHandler::new)
    );
    @Override
    public ItemStack handleRecipe(ItemStack input) {
        ItemElementData data = new ItemElementData(element,amount,attachTime);
        input.set(ModItemDataComponents.ITEM_ELEMENT_DATA,data);
        return input;
    }

    @Override
    public TurnInRecipeHandlerType<?> getType() {
        return ModTurnInRecipeHandlerTypes.ATTACH_ELEMENT.get();
    }
}
